home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Jotto ][ 1.2 / source / Shell ƒ / integrity.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.4 KB  |  129 lines  |  [TEXT/MMCC]

  1. #include "integrity.h"
  2. #include "util.h"
  3.  
  4. #define RESOURCE_FORK_DATA_LENGTH_OFFSET        8L
  5. #define RESOURCE_FORK_MAP_LENGTH_OFFSET            12L
  6. #define STORED_DATA_LENGTH_OFFSET                128L
  7. #define STORED_MAP_LENGTH_OFFSET                132L
  8. #define TAG_OFFSET                                136L
  9. #define THE_TAG                                    0x16435934
  10.  
  11. Boolean DoIntegrityCheck(Boolean *programIntegritySet)
  12. /* called first thing; can be used either to check program integrity (if it has
  13.    been installed) or to install the integrity checker (if the SHIFT key is
  14.    help down during program launch).  Returns FALSE if program integrity is
  15.    not verified; programIntegritySet is TRUE if this procedure installs the
  16.    integrity checker. */
  17. {
  18.     short            thisFile;
  19.     long            count;
  20.     long            resDataLength, checkData;
  21.     long            resMapLength, checkMap;
  22.     long            resMapOffset;
  23.     short            resAttributes;
  24.     long            tag;
  25.     KeyMap            rawKeys;
  26.     unsigned short    theKeys[8];
  27.     
  28.     *programIntegritySet=FALSE;
  29.     GetKeys(rawKeys);
  30.     Mymemcpy((Ptr)theKeys, (Ptr)rawKeys, sizeof(rawKeys));
  31.     FlushVol(0L, 0);        /* just to be on the safe side */
  32.     
  33.     /* interestingly enough, the new-and-improved functions to open a resource fork
  34.        (HOpenRF & FSpOpenRF) won't work here.  They check permissions and won't
  35.        allow write permission to an already-open resource fork -- so we won't be
  36.        able to install the integrity checker on the fly if we Do The Right Thing™.
  37.        Luckily, the old-and-inferior function (OpenRF) doesn't ask permission... */
  38.     OpenRF(LMGetCurApName(), 0, &thisFile);
  39.     
  40.     SetFPos(thisFile, 1, RESOURCE_FORK_DATA_LENGTH_OFFSET);
  41.     count=4L;
  42.     /* get length of resource data */
  43.     if (FSRead(thisFile, &count, (Ptr)(&resDataLength))!=noErr)
  44.         return FALSE;
  45.     
  46.     SetFPos(thisFile, 1, RESOURCE_FORK_MAP_LENGTH_OFFSET);
  47.     count=4L;
  48.     /* get length of resource map */
  49.     if (FSRead(thisFile, &count, (Ptr)(&resMapLength))!=noErr)
  50.         return FALSE;
  51.     
  52.     SetFPos(thisFile, 1, TAG_OFFSET);
  53.     count=4L;
  54.     FSRead(thisFile, &count, (Ptr)(&tag));    /* read long-word tag */
  55.     if (tag!=THE_TAG)    /* if not equal to our tag, integrity checker not installed */
  56.     {
  57.         if (theKeys[3]&1)    /* if SHIFT key down, install integrity checker */
  58.         {
  59.             count=4L;
  60.             SetFPos(thisFile, 1, 4L);
  61.             /* get the offset of the start of the resource map in the resource fork */
  62.             if (FSRead(thisFile, &count, (Ptr)(&resMapOffset))!=noErr)
  63.                 return FALSE;
  64.             
  65.             resMapOffset+=22L;    /* resource fork attributes at map+22 */
  66.             count=2L;
  67.             SetFPos(thisFile, 1, resMapOffset);
  68.             /* get old resource fork attributes */
  69.             if (FSRead(thisFile, &count, (Ptr)(&resAttributes))!=noErr)
  70.                 return FALSE;
  71.             
  72.             resAttributes|=0x8000;    /* lock resource fork */
  73.             count=2L;
  74.             SetFPos(thisFile, 1, resMapOffset);
  75.             /* rewrite new resource fork attributes */
  76.             /* It is a little-known fact that this trick makes the application
  77.                immune to all known viruses -- no known virus bothers to unlock the
  78.                resource fork before attempting to infect, and ABSOLUTELY NO changes
  79.                can be made to the resource fork when it's locked like this.  On the
  80.                other hand, you better be damn sure that the application has no
  81.                viruses in it when you install the integrity checker -- if it's
  82.                infected when this code is run, you won't be able to disinfect it
  83.                until you unlock the resource fork. */
  84.             if (FSWrite(thisFile, &count, (Ptr)(&resAttributes))!=noErr)
  85.                 return FALSE;
  86.             
  87.             SetFPos(thisFile, 1, STORED_DATA_LENGTH_OFFSET);
  88.             count=4L;
  89.             /* store length of resource data */
  90.             if (FSWrite(thisFile, &count, (Ptr)(&resDataLength))!=noErr)
  91.                 return FALSE;
  92.             
  93.             SetFPos(thisFile, 1, STORED_MAP_LENGTH_OFFSET);
  94.             count=4L;
  95.             /* store length of resource map */
  96.             if (FSWrite(thisFile, &count, (Ptr)(&resMapLength))!=noErr)
  97.                 return FALSE;
  98.             
  99.             SetFPos(thisFile, 1, TAG_OFFSET);
  100.             count=4L;
  101.             tag=THE_TAG;
  102.             /* store tag so we know integrity checker is installed */
  103.             if (FSWrite(thisFile, &count, (Ptr)(&tag))!=noErr)
  104.                 return FALSE;
  105.             
  106.             *programIntegritySet=TRUE;        /* so we know we've installed it */
  107.         }
  108.         
  109.         return TRUE;
  110.     }
  111.     else
  112.     {
  113.         SetFPos(thisFile, 1, STORED_DATA_LENGTH_OFFSET);
  114.         count=4L;
  115.         /* get stored resource data length */
  116.         if (FSRead(thisFile, &count, (Ptr)(&checkData))!=noErr)
  117.             return FALSE;
  118.         
  119.         SetFPos(thisFile, 1, STORED_MAP_LENGTH_OFFSET);
  120.         count=4L;
  121.         /* get stored resource map length */
  122.         if (FSRead(thisFile, &count, (Ptr)(&checkMap))!=noErr)
  123.             return FALSE;
  124.         
  125.         /* check real resource data/map lengths to stored values */
  126.         return ((resDataLength==checkData) && (resMapLength==checkMap));
  127.     }
  128. }
  129.